package com.yz.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.*;
import com.yz.common.utils.R;
import com.yz.common.entity.DiscoverForm;
import com.yz.common.validator.ValidatorUtils;
import com.yz.form.LoginForm;
import com.yz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *挖掘表单相关操作
 */
@RestController
@RequestMapping("/api")
@Api(tags="挖掘表单")
public class ApiDiscoverController {
    @Autowired
    private DiscoverService discoverService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private TestService testService;
    @Autowired
    private SampalingService sampalingService;
    @Autowired
    private StationSettingService stationSettingService;
    @Autowired
    private StationRecordService stationRecordService;
    @Autowired
    private SpecimenService specimenService;
    @Autowired
    private PotteryService potteryService;
    @Autowired
    private InclusionService inclusionService;
    @Autowired
    private GraveService graveService;
    @Autowired
    private CheckService checkService;

    @GetMapping("getDiscoverForms")
    @ApiOperation("获取挖掘表单列表")
    @Login
    public R getDiscoverForms(@RequestHeader("token")String token,@RequestParam("order")int order,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        int total =0;
        Page pages = null;
        String group = "create_at desc,id desc";
        if(order!=0){
            group = "create_at asc,id asc";
        }
        if(keyWord==null||keyWord.isEmpty()){
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type",5).where("type_form>100").eq("create_user",user.getUsername()));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type",5).where("type_form>100").eq("project_id",projectId).eq("create_user",user.getUsername()).orderBy(group));
        }else {
            total = checkService.selectCount(new EntityWrapper<CheckEntity>().eq("project_id",projectId).eq("type",5).where("type_form>100").eq("create_user",user.getUsername()).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')"));
            pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().eq("type",5).where("type_form>100").eq("project_id",projectId).eq("create_user",user.getUsername()).where("(number like '%"+keyWord+"%' or name_form like '%"+keyWord+"%')").orderBy(group));
        }
        return R.ok().put("data",pages.getRecords()).put("total",total);
    }

    @PostMapping("postGrave")
    @ApiOperation("提交或修改-墓葬发掘登记表")
    @Login
    public R postGrave(@RequestHeader("token")String token,@RequestBody GraveForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            graveService.postMe(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getGraveDetail")
    @ApiOperation("获取-墓葬发掘登记表-详情")
    @Login
    public R getGraveDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        GraveForm t = graveService.getDetail(id);
        return R.ok().put("data",t);
    }

    @DeleteMapping("deleteGrave")
    @ApiOperation("删除-墓葬发掘登记表")
    @Login
    public R deleteGrave(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            graveService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postInclusion")
    @ApiOperation("提交或修改-包含物保存状况表")
    @Login
    public R postInclusion(@RequestHeader("token")String token,@RequestBody InclusionForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            inclusionService.postInclusion(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getInclusionDetail")
    @ApiOperation("获取-包含物保存状况表-详情")
    @Login
    public R getInclusionDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
            InclusionForm t = inclusionService.getInclusionDetail(id);
            return R.ok().put("data",t);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteInclusion")
    @ApiOperation("删除-包含物保存状况表")
    @Login
    public R deleteInclusion(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            inclusionService.deleteInclusion(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postPottery")
    @ApiOperation("提交或修改-陶器登记册")
    @Login
    public R postPottery(@RequestHeader("token")String token,@RequestBody PotteryForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            potteryService.postMe(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getPotteryDetail")
    @ApiOperation("获取-陶器登记册-详情")
    @Login
    public R getPotteryDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        PotteryForm t = potteryService.getDetails(id);
        return R.ok().put("data",t);
    }

    @DeleteMapping("deletePottery")
    @ApiOperation("删除-陶器登记册")
    @Login
    public R deletePottery(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            potteryService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postSpecimen")
    @ApiOperation("提交或修改-出土器物标本登记表")
    @Login
    public R postSpecimen(@RequestHeader("token")String token,@RequestBody SpecimenForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            specimenService.postSpecimen(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getSpecimenDetail")
    @ApiOperation("获取-出土器物标本登记表-详情")
    @Login
    public R getSpecimenDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
            SpecimenForm t = specimenService.getSpecimenDetail(id);
            return R.ok().put("data",t);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteSpecimen")
    @ApiOperation("删除-出土器物标本登记表")
    @Login
    public R deleteSpecimen(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            specimenService.deleteSpecimen(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postSationRecord")
    @ApiOperation("提交或修改-全站仪测绘记录表")
    @Login
    public R postSationRecord(@RequestHeader("token")String token,@RequestBody StationRecordForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            stationRecordService.postStationRecord(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getRecordDetail")
    @ApiOperation("获取-全站仪测绘记录表-详情")
    @Login
    public R getRecordDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
            StationRecordForm t = stationRecordService.getRecordDetail(id);
            return R.ok().put("data",t);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteSationRecord")
    @ApiOperation("删除-全站仪测绘记录表")
    @Login
    public R deleteSationRecord(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            stationRecordService.deleteStationRecord(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postSationSetting")
    @ApiOperation("提交或修改-全站测绘设置表")
    @Login
    public R postSationSetting(@RequestHeader("token")String token,@RequestBody StationSettingForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            stationSettingService.postStationSetting(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getSettingDetail")
    @ApiOperation("获取-全站测绘设置表-详情")
    @Login
    public R getSettingDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
            StationSettingForm t = stationSettingService.getSettingDetail(id);
            return R.ok().put("data",t);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteSationSetting")
    @ApiOperation("删除-全站测绘设置表")
    @Login
    public R deleteSationSetting(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            stationSettingService.deleteStationSetting(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postSampaling")
    @ApiOperation("提交或修改-采样记录表")
    @Login
    public R postSampaling(@RequestHeader("token")String token,@RequestBody SampalingForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            sampalingService.postMe(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @GetMapping("getSampalingDetail")
    @ApiOperation("获取-采样记录表-详情")
    @Login
    public R getSampalingDetail(@RequestHeader("token")String token, @RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        SampalingForm t = sampalingService.getDetail(id);
        return R.ok().put("data",t);
    }

    @DeleteMapping("deleteSampaling")
    @ApiOperation("删除-采样记录表")
    @Login
    public R deleteSampaling(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            sampalingService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postDiscover")
    @ApiOperation("提交或修改-挖掘记录")
    @Login
    public R postDiscover(@RequestHeader("token")String token,@RequestBody DiscoverForm t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            discoverService.postDiscover(t,user);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
    @GetMapping("getDiscoverDetail")
    @ApiOperation("获取-挖掘记录-详情")
    @Login
    public R getDiscoverDetail(@RequestHeader("token")String token,@RequestParam("id") long id, @ApiIgnore @LoginUser UserEntity user){
        try {
            DiscoverForm data = discoverService.getDiscoverDetail(id);
           return R.ok().put("data",data);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
    @DeleteMapping("deleteDiscover")
    @ApiOperation("删除-挖掘记录")
    @Login
    public R deleteDiscover(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
           discoverService.deleteDiscover(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
}
